import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(1,10,50)
y = 2*x+1
y2 = x**2
plt.figure(1,(8,5))
label_01, = plt.plot(x,y,label='linear line')
label_02, = plt.plot(x,y2,linewidth=1.5,linestyle='--',color='LightGreen',label='square line')
# 显示图例
plt.legend(handles=[label_01,label_02,],labels=['up','down'],loc='best')
# 设置坐标轴范围
plt.xlim(2,10)
plt.ylim(10,50)
plt.xlabel('X/m')
plt.ylabel('Y/m')
x_ticks = np.linspace(2,10,9)
# y_ticks = np.linspace([10,20,30,40,50],['$india$','$russia$','$ameirca$','$jepan$','$china$'])
plt.xticks(x_ticks)
plt.yticks([10,20,30,40,50],['$india$','$russia$','$ameirca$','$jepan$','$china$'])
# 设置坐标轴不同名字和位置
ax = plt.gca() # 获取坐标轴信息
ax.spines['right'].set_color('none') # 设置边框颜色
# ax.spines['left'].set_color('red')
# ax.spines['bottom'].set_color('red')
ax.spines['top'].set_color('none')
# 调整坐标轴
# 1、设置坐标轴数字或标签位置
ax.xaxis.set_ticks_position('bottom')
# 2、设置底部坐标轴位置
ax.spines['bottom'].set_position(('data',25))
# 3、设置左侧显示y坐标刻度值或标签
ax.yaxis.set_ticks_position('left')
# 4、改变y轴位置
ax.spines['left'].set_position(('data',6))

"""
给曲线或者曲线上的点添加标记
很有用处,可以直接沿用
"""
# 5、添加标注
x0 = 7
y0 = 2*x0+1
plt.plot([x0,x0],[25,y0],'k--',linewidth=2.5)
# 6、设置点的类型
plt.scatter([x0,],[y0,],s=50,color='g')
# 7.方法一:使用annotate添加标注
plt.annotate(r'$2x+1=%s$'%y0,xy=(x0,y0),xycoords='data',xytext=(+30,+30),
             textcoords='offset points',fontsize=16,
             arrowprops=dict(arrowstyle='->',connectionstyle="arc3,rad=0.2")
)
# 方法二"使用text()函数添加注释"
plt.text(2,45,r'$this\ is\ my\ text.\mu\ \sigma_i\ \alpha_a$',
         fontdict={'size':16,'color':'r'})
plt.show()